package initialize

import (
	// "net/http"

	controllers "github.com/cn-joyconn/goadmin/controllers"
	adminControllers "github.com/cn-joyconn/goadmin/controllers/admin"
	"github.com/cn-joyconn/goadmin/middleware/auth"
	"github.com/cn-joyconn/goadmin/models/global"
	adminService "github.com/cn-joyconn/goadmin/services/admin"

	// filetool "github.com/cn-joyconn/goutils/filetool"
	// gologs "github.com/cn-joyconn/gologs"
	// middleware "github.com/cn-joyconn/goadmin/middleware"
	"github.com/gin-gonic/gin"
)

//通用
func InitCommonRouter(publicGroup *gin.RouterGroup, authGroup *auth.JoyAuthorizeGroup, permissioneGroup *auth.JoyPermissionGroup) {
	service := &adminService.AdminAppService{}
	err1, appObj := service.SelectAppByName(global.AppConf.DefaultAppName)
	if err1 != nil {
		return
	}
	controller := &controllers.CommonController{}
	publicGroup.GET("/api/CommApi/authimage", controller.AuthImage)
	authGroup.POST(appObj.PId, "/api/CommApi/fileUploadApi", "", controller.Upload)
	authGroup.POST(appObj.PId, "/api/CommApi/filesUploadApi", "", controller.UploadFiles)
}

//首页
func InitHomeRouter(publicGroup *gin.RouterGroup, authGroup *auth.JoyAuthorizeGroup, permissioneGroup *auth.JoyPermissionGroup) {
	service := &adminService.AdminAppService{}
	err1, appObj := service.SelectAppByName(global.AppConf.DefaultAppName)
	if err1 != nil {
		return
	}
	controller := &controllers.IndexController{}
	authGroup.GET(appObj.PId, "", "", controller.Index)
	authGroup.GET(appObj.PId, "/", "", controller.Index)
	authGroup.GET(appObj.PId, "/home", "", controller.Index)
	authGroup.GET(appObj.PId, "/home/index", "", controller.Index)
}

//登录
func InitAccountRouter(publicGroup *gin.RouterGroup, authGroup *auth.JoyAuthorizeGroup, permissioneGroup *auth.JoyPermissionGroup) {
	// service := &adminService.AdminAppService{}
	// err1,appObj := service.SelectAppByName(global.AppConf.DefaultAppName)
	// if err1!=nil{
	// 	return
	// }
	controller := &controllers.AccountController{}
	publicGroup.GET("/page/account/login", controller.LoginPage)
	publicGroup.POST("/api/joyconn/authorize/AuthenticationApi/dologin", controller.LoginForCookie)
	publicGroup.POST("/api/joyconn/authorize/AuthenticationApi/dologinAuth", controller.LoginForAuth)
	publicGroup.POST("/api/joyconn/authorize/AuthenticationApi/dologinout", controller.LogoutForCookie)
}

//用户
func InitAdminUserRouter(publicGroup *gin.RouterGroup, authGroup *auth.JoyAuthorizeGroup, permissioneGroup *auth.JoyPermissionGroup) {
	service := &adminService.AdminAppService{}
	err1, appObj := service.SelectAppByName(global.AppConf.DefaultAppName)
	if err1 != nil {
		return
	}
	controller := &adminControllers.UserController{}

	authGroup.GET(appObj.PId, "/page/system/account/modifyphoto", "", controller.ModifyphotoPage)
	permissioneGroup.GET(appObj.PId, "/page/system/account/UserManage", "page:system:authorize:usermanage", false, controller.ManagePage)

	authGroup.POST(appObj.PId, "/api/system/account/user/getUsers", "", controller.GetUsers)
	authGroup.GET(appObj.PId, "/api/system/account/user/getMe", "", controller.GetMe)
	authGroup.POST(appObj.PId, "/api/system/account/user/modifyPwd", "", controller.ModifyPwd)
	authGroup.POST(appObj.PId, "/api/system/account/user/modifyUserPhoto", "", controller.ModifyUserPhoto)

	permissioneGroup.GET(appObj.PId, "/api/system/account/user/getUserList", "api:system:authorize:user:getUserList", true, controller.GetUserList)
	permissioneGroup.POST(appObj.PId, "/api/system/account/user/addUser", "api:system:authorize:user:addUser", true, controller.AddUserModel)
	permissioneGroup.POST(appObj.PId, "/api/system/account/user/modifyUserModel", "api:system:authorize:user:modifyUserModel", true, controller.ModifyUserModel)
	permissioneGroup.POST(appObj.PId, "/api/system/account/user/changeUserStat", "api:system:authorize:user:changeUserStat", true, controller.ChangeUserStat)
	permissioneGroup.POST(appObj.PId, "/api/system/account/user/resetPwd", "api:system:authorize:user:resetPwd", true, controller.ResetPwd)

	upcontroller := &adminControllers.UserPermissionController{}
	authGroup.GET(appObj.PId, "/api/system/authorize/user/permission/getMyRoles", "", upcontroller.GetMyRoles)
	authGroup.GET(appObj.PId, "/api/system/authorize/user/permission/getMyRights", "", upcontroller.GetUserRights)
	authGroup.GET(appObj.PId, "/api/system/authorize/user/permission/getMyMenuByID", "", upcontroller.GetMyMenuByID)
	permissioneGroup.GET(appObj.PId, "/api/system/authorize/user/permission/getUserRoles", "api:system:authorize:user:permission:getUserRoles", true, upcontroller.SelectUserRoles)
	permissioneGroup.POST(appObj.PId, "/api/system/authorize/user/permission/update", "api:system:authorize:user:permission:update", true, upcontroller.UpdateUserRolesByPrimaryKey)
}

//权限
func InitAdminResourceRouter(publicGroup *gin.RouterGroup, authGroup *auth.JoyAuthorizeGroup, permissioneGroup *auth.JoyPermissionGroup) {
	service := &adminService.AdminAppService{}
	err1, appObj := service.SelectAppByName(global.AppConf.DefaultAppName)
	if err1 != nil {
		return
	}
	controller := &adminControllers.ResourceController{}

	authGroup.GET(appObj.PId, "/api/system/authorize/resource/getTree", "api:system:authorize:resource:getTree", controller.GetAllTree)

	permissioneGroup.GET(appObj.PId, "/page/system/authorize/resource/manage", "page:system:resource:manage", false, controller.ManagePage)

	permissioneGroup.POST(appObj.PId, "/api/system/authorize/resource/delete", "api:system:authorize:resource:delete", true, controller.DeleteByPrimaryKey)
	permissioneGroup.POST(appObj.PId, "/api/system/authorize/resource/add", "api:system:authorize:resource:add", true, controller.Insert)
	permissioneGroup.POST(appObj.PId, "/api/system/authorize/resource/update", "api:system:authorize:resource:update", true, controller.UpdateByPrimaryKey)
	permissioneGroup.GET(appObj.PId, "/api/system/authorize/resource/getone", "api:system:authorize:resource:getone", true, controller.SelectByPrimaryKey)
	permissioneGroup.GET(appObj.PId, "/api/system/authorize/resource/getAllPermission", "api:system:authorize:resource:getAllPermission", true, controller.GetAllPermission)
}

//角色
func InitAdminRoleRouter(publicGroup *gin.RouterGroup, authGroup *auth.JoyAuthorizeGroup, permissioneGroup *auth.JoyPermissionGroup) {
	service := &adminService.AdminAppService{}
	err1, appObj := service.SelectAppByName(global.AppConf.DefaultAppName)
	if err1 != nil {
		return
	}
	controller := &adminControllers.RoleController{}

	permissioneGroup.GET(appObj.PId, "/page/system/authorize/role/manage", "page:system:role:manage", false, controller.ManagePage)

	permissioneGroup.POST(appObj.PId, "/api/system/authorize/role/updateState", "api:system:authorize:role:updateState", true, controller.UpdateState)
	permissioneGroup.POST(appObj.PId, "/api/system/authorize/role/add", "api:system:authorize:role:add", true, controller.InsertRole)
	permissioneGroup.POST(appObj.PId, "/api/system/authorize/role/update", "api:system:authorize:role:update", true, controller.UpdatetRoleByPrimaryKey)
	permissioneGroup.GET(appObj.PId, "/api/system/authorize/role/getone", "api:system:authorize:role:getone", true, controller.SelectRoleByPrimaryKey)
	permissioneGroup.GET(appObj.PId, "/api/system/authorize/role/getpage", "api:system:authorize:role:getpage", true, controller.SelectRoleListByPage)
	permissioneGroup.GET(appObj.PId, "/api/system/authorize/role/getResourceIDsByRoleID", "api:system:authorize:role:getResourceIDsByRoleID", true, controller.GetResourceIDsByRoleID)
	permissioneGroup.POST(appObj.PId, "/api/system/authorize/role/updateRoleResource", "api:system:authorize:role:updateRoleResource", true, controller.UpdateRoleResource)

}

//菜单
func InitAdminMenuRouter(publicGroup *gin.RouterGroup, authGroup *auth.JoyAuthorizeGroup, permissioneGroup *auth.JoyPermissionGroup) {
	service := &adminService.AdminAppService{}
	err1, appObj := service.SelectAppByName(global.AppConf.DefaultAppName)
	if err1 != nil {
		return
	}
	controller := &adminControllers.MenuController{}

	permissioneGroup.GET(appObj.PId, "/page/system/authorize/menu/manage", "page:system:menu:manage", false, controller.ManagePage)

	permissioneGroup.POST(appObj.PId, "/api/system/authorize/menu/updateState", "api:system:authorize:menu:updateState", true, controller.UpdateState)
	permissioneGroup.POST(appObj.PId, "/api/system/authorize/menu/deleteNode", "api:system:authorize:menu:deleteNode", true, controller.DeleteMenuNode)
	permissioneGroup.POST(appObj.PId, "/api/system/authorize/menu/deleteMenu", "api:system:authorize:menu:deleteMenu", true, controller.DeleteMenu)
	permissioneGroup.POST(appObj.PId, "/api/system/authorize/menu/add", "api:system:authorize:menu:add", true, controller.InsertMenu)
	permissioneGroup.POST(appObj.PId, "/api/system/authorize/menu/update", "api:system:authorize:menu:update", true, controller.UpdatetMenuByPrimaryKey)
	permissioneGroup.GET(appObj.PId, "/api/system/authorize/menu/getMenu", "api:system:authorize:menu:getMenu", true, controller.GetMenu)
	permissioneGroup.GET(appObj.PId, "/api/system/authorize/menu/getRootByPage", "api:system:authorize:menu:getRootByPage", true, controller.GetRootByPage)
}

//应用
func InitAdminAppRouter(publicGroup *gin.RouterGroup, authGroup *auth.JoyAuthorizeGroup, permissioneGroup *auth.JoyPermissionGroup) {
	service := &adminService.AdminAppService{}
	err1, appObj := service.SelectAppByName(global.AppConf.DefaultAppName)
	if err1 != nil {
		return
	}
	controller := &adminControllers.AppController{}

	authGroup.GET(appObj.PId, "/api/system/authorize/app/getApp", "", controller.GetOne)
	authGroup.POST(appObj.PId, "/api/system/authorize/app/getApps", "", controller.GetApps)

	permissioneGroup.GET(appObj.PId, "/page/system/authorize/app/manage", "page:system:app:manage", false, controller.ManagePage)

	// permissioneGroup.POST(appObj.PId, "/api/system/authorize/app/updateState", "api:system:authorize:app:updateState", controller.UpdateState)
	permissioneGroup.POST(appObj.PId, "/api/system/authorize/app/deleteApp", "api:system:authorize:app:deleteApp", true, controller.DeleteApp)
	permissioneGroup.POST(appObj.PId, "/api/system/authorize/app/add", "api:system:authorize:app:add", true, controller.InsertApp)
	permissioneGroup.POST(appObj.PId, "/api/system/authorize/app/update", "api:system:authorize:app:update", true, controller.UpdatetApp)
	permissioneGroup.GET(appObj.PId, "/api/system/authorize/app/getByPage", "api:system:authorize:app:getByPage", true, controller.GetByPage)
}
